home *** CD-ROM | disk | FTP | other *** search
- /*
- ** This is the TScribbleDoc class definition. This class is an adaptation of
- ** the class of the same name in the book, Elements of C++ Macintosh
- ** Programming, by Dan Weston.
- **
- ** Copyright © 1991 Mark Gross
- ** (RR2, Box 84, Clayville, NY 13322);
- ** this file may be published everywhere, but no charge
- ** may be made for its use. Please contact me about any bugs found.
- ** I'm also looking for Macintosh development work, so drop me a line
- ** if you think I could help.
- */
-
- #include "TScribbleDoc.h"
- #include "magics.h"
-
- TDoc* TScribbleDoc::Init(OSType theCreator, SFReply *reply)
- {
- inherited::Init(theCreator, reply);
-
- fPattern = patGray;
- fPenSize = 2;
- fPic = NULL;
- return this;
-
- }/*end of function*/
-
- void TScribbleDoc::SetPenSize(short p)
- {
- fPenSize = p;
- }
-
- void TScribbleDoc::SetPenPat(penPat p)
- {
- fPattern = p;
- }
-
- short TScribbleDoc::GetPenSize(void)
- {
- return fPenSize;
- }
-
- penPat TScribbleDoc::GetPenPat(void)
- {
- return fPattern;
- }
-
- void TScribbleDoc::DoContent(EventRecord* theEvent)
- {
- register Point newPoint;
-
- if(fDocWindow)
- {
- SetPort(fDocWindow);
- PenSize(fPenSize, fPenSize);
-
- if(fPattern == patBlack)
- PenPat(black);
- if(fPattern == patGray)
- PenPat(gray);
- if(fPattern == patWhite)
- PenPat(white);
-
- GlobalToLocal( &theEvent->where);
- MoveTo(theEvent->where.h, theEvent->where.v);
- do
- {
- GetMouse(&newPoint);
- LineTo(newPoint.h, newPoint.v);
- } while(StillDown());
-
- fNeedtoSave = TRUE;
-
- fPic = OpenPicture(&fDocWindow->portRect);
- CopyBits(&fDocWindow->portBits, &fDocWindow->portBits,
- &fDocWindow->portRect, &fDocWindow->portRect, srcCopy, NULL);
- ClosePicture();
- }/*end of window*/
- }/*end of function*/
-
-
- void TScribbleDoc::Draw(Rect *r)
- {
-
- if(fPic != NULL)
- DrawPicture(fPic, &( (*fPic)->picFrame ) );
- }
-
- OSType TScribbleDoc::GetDocType(void)
- {
- return 'SPCT';
- }
-
-
- Boolean TScribbleDoc::WriteDocFile(short refNum)
- {
- long len;
- OSErr err;
-
- if(fDocWindow)
- {
- if(fPic != NULL)
- {
- len = GetHandleSize(fPic );
- HLock(fPic);
- err = SetFPos(refNum, fsFromStart, 0L);
- err = FSWrite( refNum, &len, *fPic);
- HUnlock(fPic);
-
- if(err == noErr)
- return TRUE;
- else
- return FALSE;
- }
- }
- }/*end of function*/
-
-
- Boolean TScribbleDoc::ReadDocFile(short refNum)
- {
- long len;
- short height, width;
- OSErr err;
- Rect r;
- PicHandle thePic;
-
- if(fDocWindow)
- {
- err = GetEOF(refNum, &len);
- thePic = (PicHandle)NewHandle(len);
- if(thePic == NULL)
- {
- ErrorAlert(rDocErrorStrings, sNoMem);
- return FALSE;
- }
-
- HLock(thePic);
- err = SetFPos(refNum, fsFromStart, 0L);
- FSRead(refNum, &len, *thePic);
- if(err == noErr)
- {
- r = (*thePic)->picFrame;
- height = r.bottom - r.top;
- width = r.right - r.left;
- r = screenBits.bounds;
- height = min(height, r.bottom - r.top - kHAdjust);
- width = min(width, r.right - r.left - kWAdjust);
- SizeWindow(fDocWindow, width, height, TRUE);
-
- fPic = thePic;
- return TRUE;
- }
- else
- {
- DisposHandle(thePic);
- return FALSE;
- }
- }/* end if window*/
- return FALSE;
- }/*end of function*/
-
-
- Boolean TScribbleDoc::DoDocMenuCommand(short menuID, short menuItem)
- {
- if(menuID == rPenMenu)
- {
- switch(menuItem)
- {
- case i1X1:
- SetPenSize(1);
- break;
- case i2X2:
- SetPenSize(2);
- break;
- case i3X3:
- SetPenSize(3);
- break;
- case iBlack:
- SetPenPat(patBlack);
- break;
- case iGray:
- SetPenPat(patGray);
- break;
- case iWhite:
- SetPenPat(patWhite);
- break;
- default:
- return FALSE;
- }/*end switch menuitem*/
- return TRUE;
- }
- else
- {
- return inherited::DoDocMenuCommand(menuID, menuItem);
- }
- }/*end of function*/
-
- Boolean TScribbleDoc::CanSaveAs(void)
- {
- return TRUE;
- }
-
- void TScribbleDoc::AdjustDocMenus(void)
- {
- MenuHandle menu;
-
- menu = GetMHandle(rPenMenu);
- CheckItem(menu,i1X1, GetPenSize() == 1);
- CheckItem(menu,i2X2, GetPenSize() == 2);
- CheckItem(menu,i3X3, GetPenSize() == 3);
- CheckItem(menu, iBlack, GetPenPat() == patBlack);
- CheckItem(menu, iGray, GetPenPat() == patGray);
- CheckItem(menu, iWhite, GetPenPat() == patWhite);
-
- inherited::AdjustDocMenus();
- }/*end of function*/
-
- void TScribbleDoc::TogglePenMenu(Boolean enable)
- {
- MenuHandle menu;
-
- menu = GetMHandle(rPenMenu);
- SetMenuAbility(menu, kEveryItem, enable);
- DrawMenuBar();
- }/*end of function*/
-
- void TScribbleDoc::Activate(void)
- {
- inherited::Activate();
- TogglePenMenu(TRUE);
- }
-
- void TScribbleDoc::Deactivate(void)
- {
- inherited::Deactivate();
- TogglePenMenu(FALSE);
- }
-
- Boolean TScribbleDoc::DoClose(void)
- {
- if(inherited::DoClose())
- {
- TogglePenMenu(FALSE);
- return TRUE;
- }
- else
- return FALSE;
- }/*end of function*/
-
-
-